#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qmessagebox.h>
#include <qfile.h>
#include <qtextstream.h>
#include <time.h>
#include "startsettings.h"
#include "talkdialog.h"
MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),ngame(0),
	ui(new Ui::MainWindow)
{
	srand((unsigned int)time(0));
    lans="English";
    ui->setupUi(this);
    ui->retranslateUi(this);
	StartSettings::mainWindow=this;
	connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(onstart()));
    connect(ui->widget,SIGNAL(changetimer()),this,SLOT(changetimer()));
    connect(ui->widget,SIGNAL(stoptimer()),this,SLOT(stoptimer()));
    connect(ui->pushButton_undo,SIGNAL(clicked()),ui->widget,SLOT(onundo()));
    ui->widget->setVT(ui->viewer,ui->timer1,ui->timer2);
    connect(ui->actionUndo,SIGNAL(triggered()),ui->widget,SLOT(onundo()));
    connect(ui->actionLoad,SIGNAL(triggered()),ui->widget,SLOT(onload()));
    connect(ui->actionSave,SIGNAL(triggered()),ui->widget,SLOT(onsave()));
    connect(ui->widget,SIGNAL(isReady(bool)),ui->actionUndo,SLOT(setEnabled(bool)));
    connect(ui->widget,SIGNAL(isReady(bool)),ui->actionSave,SLOT(setEnabled(bool)));
    connect(ui->widget,SIGNAL(isReady(bool)),ui->pushButton_undo,SLOT(setEnabled(bool)));
    connect(ui->pushButton_load,SIGNAL(clicked()),ui->widget,SLOT(onload()));
    connect(ui->pushButton_save,SIGNAL(clicked()),ui->widget,SLOT(onsave()));
    connect(ui->widget,SIGNAL(isReady(bool)),ui->pushButton_save,SLOT(setEnabled(bool)));
	connect(&opt,SIGNAL(trans(QString)),this,SLOT(ontrans(QString)));

	StartSettings::talkDialog = new TalkDialog(this);
	TalkDialog *p=StartSettings::talkDialog;
	StartSettings::talkDialog->move(this->x()+this->size().width(),this->y());
	//StartSettings::talkDialog->setProperty()
}
void MainWindow::moveEvent(QMoveEvent *)
{
	TalkDialog *p=StartSettings::talkDialog;
	StartSettings::talkDialog->move(this->x()+this->size().width(),this->y());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onstart()
{
	if(ngame)
		delete ngame;
	ngame = new NewGame(this);
	connect(ngame,SIGNAL(start()),this,SLOT(startWidget()));
	ngame->show();
}

void MainWindow::changetimer()
{
    if (nowtimer)
    {
        ui->timer1->stop();
        ui->timer2->start();
        nowtimer=0;
    }
    else
    {
        ui->timer2->stop();
        ui->timer1->start();
        nowtimer=1;
    }
}

void MainWindow::stoptimer()
{
    ui->timer1->stop();
    ui->timer2->stop();
    ui->actionSave->setEnabled(1);
    ui->pushButton_save->setEnabled(1);
}


void MainWindow::on_actionNew_triggered()
{
    onstart();
}

void MainWindow::on_actionOptions_triggered()
{
    opt.exec();
}

void MainWindow::on_actionExit_triggered()
{
    QApplication::exit();
}

void MainWindow::on_actionHelp_triggered()
{
	QString ss=":/lans/help_";
	QFile f(ss+lans+".txt");
   /* help.setText(tr("Rules:\n"
                    "1.Start:\n"
                    "Choose your colour and start a new game.\n"
                    "2.Play:\n"
                    "Black plays first, putting one stone on the chess board."
                    "Then White and Black take turns, placing two stones on two different unoccupied spaces each turn.\n"
                    "3.End:\n"
                    "The player who is the first to get six or more stones in a row (horizontally, vertically, or diagonally) wins.\n"));
	*/
	if(f.open(QIODevice::ReadOnly))
	{
		QTextStream ts(&f);
		help.setText(ts.readAll());
		help.show();
	}
}

void MainWindow::on_actionAbout_triggered()
{
	QMessageBox::about(0,tr("About Surakarta"),
                       tr("<h2>Surakarta 1.20</h2>"
                       "<p>Copyright &copy; 2011 Vance&Storm Studio."
					   "<p>This game is a Surakarta game. It will bring funs to you. :)"));
}
void MainWindow::setApp(QApplication *p)
{
	app=p;
}
void MainWindow::ontrans(QString lan)
{
	t.load(QString(":/lans/")+lan);
	lans=lan;
	app->installTranslator(&t);
	ui->retranslateUi(this);
	opt.retrans();
}
void MainWindow::startWidget()
{
	bool b;
	ui->viewer->clear();
	ui->timer1->stop();
	ui->timer2->stop();
	ui->timer1->clean();
	ui->timer2->clean();
	if(StartSettings::playerColor)
	{
		b=1;nowtimer=1;
		ui->timer1->start();
	}
	else
	{
		b=0;nowtimer=0;
		ui->timer2->start();
	}
	qDebug()<<"start widget";
	//ui->widget->start(b,ui->DifficultySlider->value());
	ui->widget->start();
}

void MainWindow::on_pushButton_talk_clicked()
{
	if(StartSettings::talkDialog && !StartSettings::talkDialog->isVisible())
	{
		StartSettings::talkDialog->show();
	}
}

void MainWindow::on_pushButton_clicked()
{

}
